package shuashuami.hb.com.fragment.agent;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.ShareCodeAdapter;
import shuashuami.hb.com.avtivity.ShareCodeHistoryActivity;
import shuashuami.hb.com.fragment.AbFragment;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.http.HttpAgentMethods;
import shuashuami.hb.com.http.HttpOnNextListener;

/**
 * <b>类   名：</b>${TYPE_NAME} <br/>
 * <b>类描述：</b> <br/>
 * <b>创建人：</b>zhangqi<br/>
 * <b>创建时间：</b>2017/6/13<br/>
 * <b>修改人：</b>zhangqi<br/>
 * <b>修改时间：</b>2017/6/13<br/>
 * <b>修改备注：</b><br/>
 *
 * @version 1.0.0<br/>
 */
public class HbAShareCodeFragm extends AbFragment {

    TextView share_btn_code_generate;

    ListView list_sharecode;

    List<String> strs;

    ShareCodeAdapter shareCodeAdapter;


    public void initView(View view) {

        strs = new ArrayList<>();
        setTitleBar("分享验证码", false);
        setRightTextView("编码列表");
        list_sharecode = (ListView) view.findViewById(R.id.list_sharecode);
        share_btn_code_generate = (TextView) view.findViewById(R.id.share_btn_code_generate);
    }

    public void initData() {

        shareCodeAdapter = new ShareCodeAdapter(strs, getActivity());
        list_sharecode.setAdapter(shareCodeAdapter);
    }

    @Override
    public void initListener() {
        setRightTextViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ShareCodeHistoryActivity.class);
                startActivity(intent);
            }
        });
        share_btn_code_generate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                generateCode();
            }
        });
    }


    public void generateCode() {
        HttpOnNextListener<List<String>> listener = new HttpOnNextListener<List<String>>() {
            @Override
            public void onNext(List<String> strings) {
                strs.clear();
                strs.addAll(strings);
                mHandler.sendEmptyMessage(0x1);
            }
        };

        HttpAgentMethods.generateCode(listener);
    }

    @Override
    public int bindView() {
        return R.layout.frag_asharecode;
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            if (what == 0x1) {
                shareCodeAdapter.notifyDataSetChanged();
            }
        }
    };


}
